package com.yanqu.road.dao.impl.activity.seacraft;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.seacraft.SeacraftSouthSeaBattleDetailData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author cwq
 * @Data 2022/3/24 18:17
 */
public class SeacraftSouthSeaBattleDetailDataDaoImpl extends TempDataObjectDao<SeacraftSouthSeaBattleDetailData> {
    public static String getSeacraftSouthSeaBattleDetailDataSql = "select * from `t_c_activity_seacraft_southsea_battle_detail` where `activityId`=? and `time`>?  ";
    public List<SeacraftSouthSeaBattleDetailData> getSeacraftSouthSeaBattleDetailDataList(int activityId,long time) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<SeacraftSouthSeaBattleDetailData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getSeacraftSouthSeaBattleDetailDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, time);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updateSeacraftSouthSeaBattleDetailDataSql = "update `t_c_activity_seacraft_southsea_battle_detail` set raidAttackMap=?,attackMap=?,useGoodMap=?,time=? where activityId=? and unionId=? and masterType=? and targetUnionId=? and round=?";
    @Override
    protected boolean updateData(SeacraftSouthSeaBattleDetailData seacraftSouthSeaBattleDetailData) {
        seacraftSouthSeaBattleDetailData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaBattleDetailData.getRaidAttackMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaBattleDetailData.getAttackMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaBattleDetailData.getUseGoodMap())));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftSouthSeaBattleDetailData.getTime()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaBattleDetailData.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, seacraftSouthSeaBattleDetailData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaBattleDetailData.getMasterType()));
        para.put(i++, new DbParameter(Types.VARCHAR, seacraftSouthSeaBattleDetailData.getTargetUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaBattleDetailData.getRound()));
        if (getDb().executeNoneQuery(updateSeacraftSouthSeaBattleDetailDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSeacraftSouthSeaBattleDetailDataSql = "insert into `t_c_activity_seacraft_southsea_battle_detail`(activityId,unionId,masterType,targetUnionId,round,raidAttackMap,attackMap,useGoodMap,time) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(SeacraftSouthSeaBattleDetailData seacraftSouthSeaBattleDetailData) {
        seacraftSouthSeaBattleDetailData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaBattleDetailData.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, seacraftSouthSeaBattleDetailData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaBattleDetailData.getMasterType()));
        para.put(i++, new DbParameter(Types.VARCHAR, seacraftSouthSeaBattleDetailData.getTargetUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaBattleDetailData.getRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaBattleDetailData.getRaidAttackMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaBattleDetailData.getAttackMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaBattleDetailData.getUseGoodMap())));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftSouthSeaBattleDetailData.getTime()));
        if (getDb().executeNoneQuery(insertSeacraftSouthSeaBattleDetailDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected SeacraftSouthSeaBattleDetailData parseData(ResultSet rs) throws SQLException {
        SeacraftSouthSeaBattleDetailData seacraftSouthSeaBattleDetailData = new SeacraftSouthSeaBattleDetailData();
        seacraftSouthSeaBattleDetailData.setActivityId(rs.getInt("activityId"));
        seacraftSouthSeaBattleDetailData.setUnionId(rs.getString("unionId"));
        seacraftSouthSeaBattleDetailData.setMasterType(rs.getInt("masterType"));
        seacraftSouthSeaBattleDetailData.setTargetUnionId(rs.getString("targetUnionId"));
        seacraftSouthSeaBattleDetailData.setRound(rs.getInt("round"));
        seacraftSouthSeaBattleDetailData.setRaidAttackMap(JSON.parseObject(rs.getString("raidAttackMap"), new TypeReference<java.util.Map<java.lang.Long, com.yanqu.road.entity.activity.seacraft.SeacraftSouthSeaBattleDetailInfo>>() {
        }));
        seacraftSouthSeaBattleDetailData.setAttackMap(JSON.parseObject(rs.getString("attackMap"), new TypeReference<java.util.Map<java.lang.Long, com.yanqu.road.entity.activity.seacraft.SeacraftSouthSeaBattleDetailInfo>>() {}));
        seacraftSouthSeaBattleDetailData.setUseGoodMap(JSON.parseObject(rs.getString("useGoodMap"), new TypeReference<java.util.Map<java.lang.Long, com.yanqu.road.entity.activity.seacraft.SeacraftSouthSeaBattleDetailInfo>>() {}));
        seacraftSouthSeaBattleDetailData.setTime(rs.getLong("time"));

        return seacraftSouthSeaBattleDetailData;
    }

}
